﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Administration_UIControls_AccordionMenu : System.Web.UI.UserControl
{
    string cls = string.Empty;
    string cl = string.Empty;
    string nojs = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

        liMenu.Text = string.Format("<ul id=\"mainnav\">{0}</ul>", DisplaySiteMapLevel());
    }

    /// <summary>
    /// Loads menudata
    /// </summary>
    /// <returns></returns>
    private string DisplaySiteMapLevel()
    {
        SiteMapNode node = SiteMap.Provider.FindSiteMapNode("~/administration/");
        if (node == null)
            throw new Exception("No childnodes");

        SiteMapNodeCollection nodes = node.ChildNodes;
        return GetSiteMapLevel(nodes);
    }

    /// <summary>
    /// Iterates all nodes, recursively if needed
    /// </summary>
    /// <param name="nodes"></param>
    /// <returns></returns>
    private string GetSiteMapLevel(SiteMapNodeCollection nodes)
    {
        string output = String.Empty;

        foreach (SiteMapNode node in nodes)
        {

            if (node.Title.Equals("Admin"))
                GetSiteMapLevel(node.ChildNodes);

            if (node.Url.Contains("administration"))
            {
                cls = string.Empty;
                cl = string.Empty;
                nojs = string.Empty;

                cls = "class=\"nav-top-item\"";

                if (node.HasChildNodes && IsInPath(node))
                    cls = "class=\"nav-top-item current\"";

                if (SiteMap.CurrentNode.Equals(node))
                    cl = "class=\"nav-top-item current\"";


                if (node.HasChildNodes)
                {
                    output += string.Format("\n<li><a href=\"{0}\"{2}>{1}</a>", node.Url, node.Title.Replace("&", "&amp;"), cls);
                }
                else
                {
                    if (node.ParentNode.Equals(SiteMap.Provider.FindSiteMapNode("/administration/")))
                    {
                        cl = " class=\"nav-top-item\"";
                        nojs = " class=\"nav-top-item no-submenu\"";

                        if (node.Equals(SiteMap.CurrentNode))
                            nojs = " class=\"nav-top-item current\"";
                    }

                    output += string.Format("\n<li><a{3} href=\"{0}\"{2}>{1}</a>", node.Url, node.Title.Replace("&", "&amp;"), cl, nojs);
                }

                // Add any children levels, if needed (recursively)
                if (node.HasChildNodes)
                    output += string.Format("\n<ul>\n{0}\n</ul>\n", GetSiteMapLevel(node.ChildNodes));

                output += "</li>\n";
            }


        }


        return output;
    }
      


    /// <summary>
    /// Find a node in same path.
    /// In this way we can alter any level of nodes and select the parent node if a subitem is selected.
    /// Very handy when using a topmenu with a submenu and want to select the menu above a subitem.
    /// </summary>
    /// <param name="node">The node to find the current node from.</param>
    /// <returns>True when found the node</returns>
    private bool IsInPath(SiteMapNode node)
    {
        foreach (SiteMapNode child in node.ChildNodes)
        {
            if (child.Equals(SiteMap.CurrentNode))
                return true;
            else
                IsInPath(child); //Iterate until it's found.
        }
        return false;
    }
}
